import { selectListDict } from "@/api/dictionary"
import {
    getCustomSetting,
    setCustomSetting,
    setLocCategoryType
} from "@/utils/storage"
import { getAllCategory } from '@/api/category'
import { createTreeData } from '@ruifox-cms/utils'

const state = {
    categoryType: [], // 栏目类型字典值
    categoryTree: [], //用户的栏目树(不分站点)
    categoryFlat: [], // 用户栏目树平铺(不分站点)
    siteCategoryTree: [], // 当前站点的栏目树
    categoryLoading: false,
    customSetting: getCustomSetting() ? getCustomSetting() : { // 内容管理左下角自定义设置相关
        common_category: 0,
        depart_category: 1,
        site_category: 1,
        special_category: 0,
    },
    categoryDrawer: false, // 栏目设置抽屉打开标志（包括科室栏目设置）
    categoryId: null, // 当前编辑栏目的id
    categoryPid: null, // 当前编辑栏目所属的父id
    checkedCategories: new Set() // 存储选中的栏目 id
}

const mutations = {
    SET_CATEGORY_TYPE: (state,list) => {
        setLocCategoryType(list)
        state.categoryType = JSON.parse(JSON.stringify(list))
    },
    SET_CUSTOM_SETTING: (state,setting) => {
        setCustomSetting(setting)
        state.customSetting = JSON.parse(JSON.stringify(setting))
    },
    // 设置栏目树
    SET_CATEGORY_TREE: (state, value) => {
        state.categoryTree = value
    },
    // 设置当前站点的栏目树
    SET_SITE_CATEGORY_TREE: (state, {treeData, siteId}) => {
        const data = treeData ? treeData : state.categoryTree
        state.siteCategoryTree = data.filter(item => item.siteId === siteId)
    },
    // 设置用户栏目树平铺
    SET_CATEGORY_FLAT: (state, value) => {
        state.categoryFlat = value
    },
    // 设置栏目加载状态 
    SET_CATEGORY_LOADING: (state, value) => {
        state.categoryLoading = value
    },
    // 设置栏目设置抽屉打开标志
    SET_CATEGORY_DRAWER: (state, value) => {
        state.categoryDrawer = value
    },
    // 设置当前编辑栏目的id
    SET_CATEGORY_ID: (state, value) => {
        state.categoryId = value
    },
    // 设置当前编辑栏目所属的父id
    SET_CATEGORY_PID: (state, value) => {
        state.categoryPid = value
    },
    // 添加批量操作勾选
    ADD_CHECKED_CATEGORY(state, id) {
        state.checkedCategories.add(id)
    },
    // 取消批量操作勾选
    REMOVE_CHECKED_CATEGORY(state, id) {
        state.checkedCategories.delete(id)
    },
    // 清空已选栏目
    CLEAR_CHECKED_CATEGORIES(state) {
        state.checkedCategories.clear()
        state.categoryFlat.forEach(item => item.checked = false)
    }
}

const actions = {
    // 获取栏目数据
    updateCategory(context) {
        context.commit('SET_CATEGORY_LOADING', true)
        return new Promise((resolve, reject) => {
            getAllCategory().then(res => {
                if(res.code === 20000) {
                    const treeData = createTreeData(res.data)
                    context.commit('SET_CATEGORY_TREE', treeData)
                    context.commit('SET_SITE_CATEGORY_TREE', {treeData, siteId: context.rootState.site.currentSite.id})
                    context.commit('SET_CATEGORY_FLAT', res.data)
                    context.commit('SET_CATEGORY_LOADING', false)
                    resolve(treeData)
                } else {
                    reject('error')
                }
            }).catch(err => {
                context.commit('SET_CATEGORY_LOADING', false)
                reject(err)
            })
        })
    },
    // 更新类型
    updateCategoryType({ commit }){
        return new Promise((resolve, reject) => {
            // 数据字典中获取栏目类型
            selectListDict({ dictionaryGroupId: 6 }).then(res => {
                if(res.code === 20000){
                    res.data.forEach(item => item.dataValue = Number(item.dataValue))
                    commit('SET_CATEGORY_TYPE', res.data)
                    resolve(res.data)
                }else {
                    reject('error')
                }
            })
        })
    }
}

export default {
    namespaced: true, //命名空间
    state,
    mutations,
    actions,
}
